package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.ConstractRecord;
import com.ruoyi.system.domain.Contract;
import com.ruoyi.system.mapper.ContractMapper;
import com.ruoyi.system.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ConstractInstituteMapper;
import com.ruoyi.system.domain.ConstractInstitute;
import com.ruoyi.system.service.IConstractInstituteService;
import com.ruoyi.common.core.text.Convert;

/**
 * 总院信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-04
 */
@Service
public class ConstractInstituteServiceImpl implements IConstractInstituteService 
{

    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ConstractInstituteMapper constractInstituteMapper;

    /**
     * 查询总院信息
     * 
     * @param id 总院信息主键
     * @return 总院信息
     */
    @Override
    public ConstractInstitute selectConstractInstituteById(Long id)
    {
        return constractInstituteMapper.selectConstractInstituteById(id);
    }

    /**
     * 查询总院信息列表
     * 
     * @param constractInstitute 总院信息
     * @return 总院信息
     */
    @Override
    public List<ConstractInstitute> selectConstractInstituteList(ConstractInstitute constractInstitute)
    {
        return constractInstituteMapper.selectConstractInstituteList(constractInstitute);
    }

    /**
     * 新增总院信息
     * 
     * @param constractInstitute 总院信息
     * @return 结果
     */
    @Override
    public int insertConstractInstitute(ConstractInstitute constractInstitute)
    {
        Contract contract = contractMapper.selectContractById(constractInstitute.getConstractId());

        contract.setPayMoney(DoubleUtil.sum(contract.getPayMoney(), constractInstitute.getPayMoney()));
        contractMapper.updateContract(contract);
        return constractInstituteMapper.insertConstractInstitute(constractInstitute);
    }

    /**
     * 修改总院信息
     * 
     * @param constractInstitute 总院信息
     * @return 结果
     */
    @Override
    public int updateConstractInstitute(ConstractInstitute constractInstitute)
    {
        ConstractInstitute constractRecord1 = constractInstituteMapper.selectConstractInstituteById(constractInstitute.getId());
        Double oldMoney = constractRecord1.getPayMoney();
        Contract contract = contractMapper.selectContractById(constractRecord1.getConstractId());
        contract.setPayMoney(DoubleUtil.sum(contract.getPayMoney(), DoubleUtil.cut(constractInstitute.getPayMoney() ,oldMoney)));
        contractMapper.updateContract(contract);
        return constractInstituteMapper.updateConstractInstitute(constractInstitute);
    }

    /**
     * 批量删除总院信息
     * 
     * @param ids 需要删除的总院信息主键
     * @return 结果
     */
    @Override
    public int deleteConstractInstituteByIds(String ids)
    {
        return constractInstituteMapper.deleteConstractInstituteByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除总院信息信息
     * 
     * @param id 总院信息主键
     * @return 结果
     */
    @Override
    public int deleteConstractInstituteById(Long id)
    {
        return constractInstituteMapper.deleteConstractInstituteById(id);
    }
}
